package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 劳改基金会
 * @author kd-15
 *
 */
public class LaogaiAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://observechina.laogai.org/content/[0-9]*-[0-9]*/[[A-Za-z0-9]*]?[%[A-Z0-9]*]*",
		"http://observechina.laogai.org/node/[0-9]*"
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("div.region-inner h1").text();
		if(title.contains("-")) title=StringUtils.substringBefore(title, "-");
		content=HtmlCleaner.getContentHtml(url,doc.select("div.article-content"));
		String dateStr = StringUtils.substringBetween(url, "content/", "-");
		if(StringUtils.isNotBlank(dateStr)){
			date =DateUtils.matchDate(dateStr.substring(4)+dateStr.substring(0, 4));
		}
		if(doc.select("div.region-inner a").size()>0){
			author = doc.select("div.region-inner a").get(0).text();
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
